
#define MM_NOMS_INCLUDED 1

#define TASK_SHOW_NOMS 4000

#if !defined MM_GLOBALS_INCLUDED
#include "include/mm_globals.inl"
#endif


//{ CVARS
new gCvarNom;
new gCvarNomMax;
new gCvarNomCheckMin;
new gCvarNomCheckMax;
new gCvarNomCheckLastMaps;
new gCvarShowNoms;
//}

new gPlayersNom[MAX_PLAYERS];
new gNomsCount;
new allNoms[MAP_NAME_LEN * MAX_MAPS+MAX_MAPS];

stock plugin_init_noms() {
	gCvarNom = register_cvar("mm_nom", "1");
	gCvarNomMax = register_cvar("mm_nom_max", "3");
	gCvarNomCheckMin = register_cvar("mm_nom_check_min", "1");
	gCvarNomCheckMax = register_cvar("mm_nom_check_max", "1");
	gCvarNomCheckLastMaps = register_cvar("mm_nom_check_last", "6");
	gCvarShowNoms = register_cvar ("mm_noms_show","1");
	
	register_clcmd("say noms", "saidNoms");
	register_clcmd("say nominations", "saidNoms");
	register_clcmd("noms", "saidNoms");
	register_clcmd("nominations", "saidNoms");
	
	
	register_clcmd("say", "handleSay");
	new i;
	for(i=0;i<MAX_PLAYERS;i++)
		gPlayersNom[i] = NO_MAP;
}

public roundStart_noms(){
	setShowNomsTask();
}

stock setShowNomsTask(){	
	if(get_pcvar_num(gCvarShowNoms)){
		if(!task_exists(TASK_SHOW_NOMS)){
			set_task(1.0, "showNoms",  TASK_SHOW_NOMS,_,_,"b");
		}
	} else {
		if(task_exists(TASK_SHOW_NOMS)){
			remove_task(TASK_SHOW_NOMS);
		}
	}
}

public showNoms(){	
	if(gNomsCount > 0) {
		set_hudmessage(255,255,255,-1.0,0.01,0, 1.0, 1.0, 0.1, 0.2, 13);
		show_hudmessage(0, allNoms);
	}
	return PLUGIN_CONTINUE;
} 

public saidNoms(id){
	if(!get_pcvar_num(gCvarActive))
		return PLUGIN_CONTINUE;
	if(!get_pcvar_num(gCvarNom)){
		client_print(id, print_chat, "%L", LANG_PLAYER, "NO_NOMS_ALLOWED");
		return PLUGIN_HANDLED;
	}
	if(gNomsCount == 0){
		client_print(id, print_chat, "%L", LANG_PLAYER ,"NO_MAPS_NOMINATED");
		return PLUGIN_HANDLED;
	}
	client_print(id, print_chat, "%L",LANG_PLAYER, "NOMS_COUNT",  gNomsCount, get_pcvar_num(gCvarNomMax));
	
	client_print(id, print_chat, "%L:", LANG_PLAYER, "NOMINATED_MAPS", allNoms);
	return PLUGIN_HANDLED;
}

stock client_connect_noms(id) {
	gPlayersNom[id] = NO_MAP;
}

stock client_disconnect_noms(id){
	nominateMap(id, NO_MAP, gMapVoteStarted?0:1);
}
stock refreshNoms(){
	new mapId;
	new map[MAP_NAME_LEN];
	allNoms[0] = 0;
	for(new i=0;i<MAX_PLAYERS;i++) {
		mapId = gPlayersNom[i];
		if(mapId != NO_MAP){
			getMapNameById(mapId, map);
			format(allNoms, charsmax(allNoms), "%s%s%s",allNoms, (allNoms[0]?", ":""), map);
		}
	}
}

public handleSay(id){
	if(!get_pcvar_num(gCvarActive))
		return PLUGIN_CONTINUE;
	if(!get_pcvar_num(gCvarNom))
		return PLUGIN_CONTINUE;
	
	new said[MAP_NAME_LEN];
	read_args(said, MAP_NAME_LEN -1);
	remove_quotes(said);
	strtolower(said);
	
	new mapId;
	new minPlayers, maxPlayers;
	new mapRealName[MAP_NAME_LEN];
	new checkMin = get_pcvar_num(gCvarNomCheckMin);
	new checkMax = get_pcvar_num(gCvarNomCheckMax);
	new checkLastMap = get_pcvar_num(gCvarNomCheckLastMaps);
	new valid = isMapValid(said, checkMin, checkMax, checkLastMap, mapId, mapRealName, minPlayers, maxPlayers);
	new name[MAX_NAME_LEN];
	if(valid == MAP_NOT_PRESENT)
		return PLUGIN_CONTINUE;
	if(gMapVoteStarted){
		client_print(id, print_chat, "%L", LANG_PLAYER, "NO_NOMS_WHILE_VOTING");
		return PLUGIN_CONTINUE;
	}
	new maxNominations = get_pcvar_num(gCvarNomMax);
	if(maxNominations  <= gNomsCount){
		client_print(id, print_chat, "%L",LANG_PLAYER, "MAX_NOMS_REACHED",  maxNominations);
	} else {
		switch(valid) {
			case MAP_VALID_FOR_NOMINATION:
			{
				new player;
				if(isMapNominated(mapId, player)){
					get_user_name(player, name, MAX_NAME_LEN -1);
					client_print(id, print_chat, "%L", LANG_PLAYER, "MAP_ALREADY_NOMINATED_BY",  mapRealName, name);
				} else {
					get_user_name(id, name, MAX_NAME_LEN -1);
					client_print(0, print_chat, "%L",LANG_PLAYER, "MAP_NOMINATED",  name, mapRealName);
					nominateMap(id, mapId);
				}
			}
			case MAP_NOT_ENOUGH_PLAYERS: {
				client_print(id, print_chat, "%L", LANG_PLAYER, "MIN_PLAYERS_NEEDED",  mapRealName, minPlayers);
				client_print(id, print_chat, "%L", LANG_PLAYER, "INFO_LISTMAPS");
			}
			case MAP_TOO_MANY_PLAYERS:{
				client_print(id, print_chat, "%L", LANG_PLAYER, "TOO_MANY_PLAYERS",  mapRealName, maxPlayers);
				client_print(id, print_chat, "%L", LANG_PLAYER, "INFO_LISTMAPS");
			}
			case MAP_LAST_PLAYED:{
				client_print(id, print_chat, "%L", LANG_PLAYER, "MAP_WAS_LAST_PLAYED", mapRealName);
				client_print(id, print_chat, "%L", LANG_PLAYER, "INFO_RECENTMAPS");
			}			
			case MAP_CURRENT:
				client_print(id, print_chat, "%L", LANG_PLAYER, "CANT_NOMINATE_CURRENT");
		}
	}
	return PLUGIN_CONTINUE;
}

stock nominateMap(player, mapId, showInfo = 1){
	new oldMapId = gPlayersNom[player];
	if(oldMapId > NO_MAP) {
		if(gNomsCount >0)
			gNomsCount--;
		if(showInfo ){
			new oldMap[MAP_NAME_LEN];
			getMapNameById(oldMapId, oldMap);
			client_print(0, print_chat, "%L",LANG_PLAYER, "MAP_NOT_NOMINATED",  oldMap);
		}
	}
	gPlayersNom[player] = mapId;
	if(mapId>NO_MAP)
		gNomsCount++;
	refreshNoms();
}

stock isMapNominated(mapId, &player){
	player = -1;
	for(new i=0;i<MAX_PLAYERS;i++){
		if(gPlayersNom[i]== mapId){
			player = i;
			return 1;
		}
	}
	return 0;
}
public getNominatedMaps(maps[], maxMaps, &num){
	num = min(maxMaps, gNomsCount);
	new count =0;
	new map;
	for(new i=0;i<MAX_PLAYERS;i++){
		if(count>=num)
			break;
		map = gPlayersNom[i];
		if(map != NO_MAP){
			maps[count] = map;
			count++;
		}
	}
}
